package com.wisezone.grademanager;

import java.util.ArrayList;
import java.util.List;

/**
 * 学生管理模块
 * */
public class StudentManager {
	public static List<Student> studentAllLists = new ArrayList<>(); //存储所有学生对象的容器
	
	public StudentManager() {
//		Student s1 = new Student(1001,"小明",22,"男");
//		Student s2 = new Student(1002,"小丽",21,"女");
//		Student s3 = new Student(1003,"张三",23,"男");
//		
//		studentAllLists.add(s1);
//		studentAllLists.add(s2);
//		studentAllLists.add(s3);
	}
	
	public void startStudentManager() {
		System.out.println("=====学生管理主界面========");
		boolean isBack = true;
		while (isBack) {
			System.out.println("1 添加学生, 2 显示学生 , 3 查找学生 , 4 返回班级系统主界面");
			int common = Utils.inputInt();
			switch (common) {
			case 1:
				addStudent();
				break;
			case 2:
				show();
				break;
			case 3:
				System.out.println("查找学生");
				break;
			case 4:
				isBack = false;
				break;
			}
		}
	}
	/**
	 * 添加学生
	 */
	public void addStudent() {
		System.out.println("请输入姓名:");
		String studentName = Utils.inputString();
		System.out.println("请输入学号:");
		int number = Utils.inputInt();
		System.out.println("请输入年龄:");
		int age = Utils.inputInt();
		System.out.println("请输入性别:");
		String sex = Utils.inputString();
		
		System.out.println("请输入班级:");
		String gradeName = Utils.inputString();
		
		Student student = new Student(number,studentName,age,sex);
	    //student添加到班级对象存储学生容器中
		//1. 根据班级名称找到班级对象
		Grade newGrade = findGradeByName(gradeName);
		// 2. 将学生对象添加到班级容器中
		newGrade.getStudentLists().add(student); //一对多(班级到学生)单向关联
		student.setGrade(newGrade); //多对一（学生到班级）单向关联
	
		studentAllLists.add(student);
		
		System.out.println("添加学生成功!");

	}
	/**
	 * 根据班级名查找班级
	 * @return
	 */
	public Grade findGradeByName(String gradeName) {
		Grade newGrade = null;
		for (Grade grade : GradeManager.gradeAllLists) {
			if(gradeName.equals(grade.getName())) {
				newGrade = grade;
				break;
			}
		}
		return newGrade;
	}
	/**
	 * 显示所有学生
	 */
	public void show() {
		System.out.println("姓名\t学号\t年龄\t性别\t班级");
		for (Student student : studentAllLists) {
			Grade grade = student.getGrade();
			System.out.println(student.getName()+"\t"+student.getNumber()+"\t"+student.getAge()+"\t"+student.getSex()+"\t"+grade.getName());
		}
	}
	
	/**
	 * 查找学生所在班级
	 */
	public Grade getGradeNameByStudent(Student student) {
		Grade newGrade = null;
		boolean isBreak = false; //找到班级结束外层循环 isBreak = true;
		List<Grade> gradeLists = GradeManager.gradeAllLists;
		for (Grade grade : gradeLists) {
			List<Student> studentLists = grade.getStudentLists();
			for (Student stu : studentLists) {
				if(stu.getName().equals(student.getName())){
					newGrade = grade;
					isBreak = true;
					break; //结束内层循环
				}
			}
			//找到班级结束外层循环
			if(isBreak == true) {
				break;
			}
		}
		
		return newGrade;
	}
	
}
